//
//  SISSMyEnrollRentOutViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/19.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMyEnrollRentOutViewController.h"
#import "SISSCommonRentCell.h"
#import "SISSEnrollRentOutDetailViewController.h"
#import "SISSNetworkingManager+Mine.h"



static NSString *myEnrollRentOutCellReuseID = @"myEnrollRentOutCellReuseID";
@interface SISSMyEnrollRentOutViewController ()

@property (weak, nonatomic) IBOutlet SISSBaseTableView *tableView;

/** 房源数组 */
@property (copy, nonatomic) NSArray<SISSRentOutModel *> *rentOutArr;

@end

@implementation SISSMyEnrollRentOutViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestMyEnrollRentOutResourceData];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.rentOutArr.count];
    return self.rentOutArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSCommonRentCell *cell = [tableView dequeueReusableCellWithIdentifier:myEnrollRentOutCellReuseID forIndexPath:indexPath];
    cell.isHideButton = YES;
    
    cell.rentOutModel = self.rentOutArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SISSEnrollRentOutDetailViewController *vc = [[SISSEnrollRentOutDetailViewController alloc] init];
    vc.title = @"出租详情";
    vc.isChange = YES;
    vc.rentOutModel = self.rentOutArr[indexPath.row];
    vc.deleteComplete = ^{
        
        [self requestMyEnrollRentOutResourceData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    // tableView相关
    // 自动行高
    self.tableView.estimatedRowHeight = 80;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    // 去掉cell系统的线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 注册cell
    [self.tableView registerClass:[SISSCommonRentCell class] forCellReuseIdentifier:myEnrollRentOutCellReuseID];
}

#pragma mark - 上下拉刷新加载方法
- (void)upDownLoadData {
    
    [self requestMyEnrollRentOutResourceData];
}




#pragma mark - 6.3.3 获取登记的线下出租
- (void)requestMyEnrollRentOutResourceData {
    
    WeakSelf
    [kNetworkManager requestOfflineRentOutWithsuccess:^(NSArray<SISSRentOutModel *> *rentOutResources) {
        weakSelf.rentOutArr = rentOutResources;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            
            [weakSelf.tableView.mj_header endRefreshing];
        }];
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

@end
